package com.bwie.controller;

import cn.hutool.log.Log;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bwie.pojo.Specification;
import com.bwie.pojo.SpecificationOption;
import com.bwie.service.SpecificationOptionService;
import com.bwie.service.SpecificationService;
import com.bwie.utils.R;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author : LeiMingkun
 * @date : 2024-08-20 19:49
 * @description : SpecificationController
 **/
@RestController
@RequestMapping("/manage/specification")
public class SpecificationController {
    @Resource
    private SpecificationService specificationService;
    @Resource
    private SpecificationOptionService specificationOptionService;

    @RequestMapping("/list/{startPage}/{endPage}")
    public R list(@RequestParam("specName")String specName, @PathVariable Integer startPage,@PathVariable Integer endPage){
        QueryWrapper queryWrapper = new QueryWrapper();
        if(StringUtils.isNotBlank(specName)){
            queryWrapper.like("spec_name",specName);
        }
        Page<Specification> page = new Page<Specification>(startPage,endPage);
        Page reponsePage = specificationService.page(page, queryWrapper);
        Map<String, Object> map = new HashMap<>();
        map.put("list",reponsePage.getRecords());
        map.put("total",reponsePage.getTotal());
        return R.success(map);
    }

    @RequestMapping("/add")
    public R add(@RequestBody Specification specification){
        return specificationService.add(specification);
    }

    @RequestMapping("/update")
    public R update(@RequestBody Specification specification){
        return specificationService.updates(specification);
    }

    @RequestMapping("/findById/{id}")
    public R update(@PathVariable Long id){
        Specification specification = this.specificationService.getById(id);
        LambdaQueryWrapper<SpecificationOption> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SpecificationOption::getSpecId,id);
        List<SpecificationOption> list = this.specificationOptionService.list(lambdaQueryWrapper);
        specification.setSpecificationOptionList(list);
        return R.success(specification);
    }

    @RequestMapping("/delById/{id}")
    public R del(@PathVariable Long id){
        return specificationService.del(id);
    }

    @RequestMapping("/delByBath")
    public R delByBath(@RequestBody List<Integer> idsList){
        return specificationService.delByBath(idsList);
    }


}